-- git signs highlights text that has changed since the list git commit,
-- and also lets you interactively stage & unstage hunks in a commit.
-- 显示 Git 更改的标记

return {
  {
    "lewis6991/gitsigns.nvim",
    build = ":TSUpdate",
    event = "LazyFile",
    opts = {
      signs = {
        add = { text = "+" },
        change = { text = "*" },
        delete = { text = "_" },
        topdelete = { text = "‾" },
        changedelete = { text = "~" },
        untracked = { text = "?" },
      },
      signs_staged = {
        add = { text = "+" },
        change = { text = "*" },
        delete = { text = "_" },
        topdelete = { text = "‾" },
        changedelete = { text = "~" },
        untracked = { text = "?" },
      },
      signs_staged_enable = true,
      signcolumn = true, -- Toggle with `:Gitsigns toggle_signs`
      numhl = true, -- Toggle with `:Gitsigns toggle_numhl`         行号带颜色
      linehl = false, -- Toggle with `:Gitsigns toggle_linehl`       为修改行带背景高亮
      word_diff = false, -- Toggle with `:Gitsigns toggle_word_diff`
      watch_gitdir = {
        follow_files = true,
      },
      preview_config = {
        -- Options passed to nvim_open_win
        style = "minimal",
        relative = "cursor",
        row = 0,
        col = 0,
      },
      on_attach = function(buffer)
        local gs = package.loaded.gitsigns

        local function map(mode, l, r, desc)
          vim.keymap.set(mode, l, r, { buffer = buffer, desc = desc })
        end

        -- stylua: ignore start
        map("n", "]h", function()
          if vim.wo.diff then
            vim.cmd.normal({ "]c", bang = true })
          else
            gs.nav_hunk("next")
          end
        end, "Next Hunk")
        map("n", "[h", function()
          if vim.wo.diff then
            vim.cmd.normal({ "[c", bang = true })
          else
            gs.nav_hunk("prev")
          end
        end, "Prev Hunk")
        map("n", "]H",         function() gs.nav_hunk("last") end,            "Last Hunk")
        map("n", "[H",         function() gs.nav_hunk("first") end,           "First Hunk")
        map("n", "<leader>gS", gs.stage_buffer,                               "Stage Buffer")
        map({"n","v"}, "<leader>gs", ":Gitsigns stage_hunk<CR>", "Stage Hunk")
        map("n", "<leader>gu", gs.undo_stage_hunk,                            "Undo Stage Hunk")
        map("n", "<leader>gR", gs.reset_buffer,                               "Reset Buffer")
        map({"n","v"}, "<leader>gr", ":Gitsigns reset_hunk<CR>", "Reset Hunk")
        map("n", "<leader>gp", gs.preview_hunk_inline,                        "Preview Hunk Inline")
        map("n", "<leader>gb", function() gs.blame_line({ full = true }) end, "Blame Line")
        map("n", "<leader>gB", function() gs.blame() end,                     "Blame Buffer")
        map("n", "<leader>gd", gs.diffthis,                                   "Diff This")
        map("n", "<leader>gD", function() gs.diffthis("~") end,               "Diff This ~")
        map("n", "<leader>ga", function() gs.toggle_linehl() end,             "切换显示/隐藏hunk修改部分的背景色")
        map("n", "<leader>ge", function() gs.toggle_deleted() end,            "切换显示/隐藏hunk修改内容")
        map("n", "<leader>gw", function() gs.toggle_word_diff() end,          "切换显示/隐藏word修改内容")
      end,
    },
  },
}
